package Q258

class Main {

}

/**
 * 各数相加
 * @author 23737
 * @date 2022.5.6
 */
fun main(){
    val num = 38
    println(Solution().addDigits(num))
}

class Solution {
    fun addDigits(num: Int): Int {
        var num = num
        if(num == 0) return 0
        while(num >= 10){
            num = helper(num.toString())
        }
        return num
    }

    private fun helper(s: String): Int {
        var res = 0
        for (c in s.toCharArray()) {
            res += c - '0'
        }
        return res
    }
}